﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Emc.Documentum.FS.Services.Core;
using Emc.Documentum.FS.DataModel.Core;
using Emc.Documentum.FS.DataModel.Core.Query;
using Emc.Documentum.FS.DataModel.Core.Properties;
using Emc.Documentum.FS.DataModel.Core.Profiles;
using Emc.Documentum.FS.Runtime;
using Emc.Documentum.FS.Runtime.Context;
using Emc.Documentum.FS.DataModel.Core.Context;
using Emc.Documentum.FS.DataModel.Core.Content;

namespace StreamingClient2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void initializeContext()
        {
            ContextFactory contextFactory = ContextFactory.Instance;
            serviceContext = contextFactory.NewContext();

            RepositoryIdentity repoId = new RepositoryIdentity();
            RepositoryIdentity repositoryIdentity =
                new RepositoryIdentity(defaultRepository, userName, password, "");
            serviceContext.AddIdentity(repositoryIdentity);

            ContentTransferProfile contentTransferProfile = new ContentTransferProfile();
            contentTransferProfile.TransferMode = ContentTransferMode.MTOM;
            contentTransferProfile.Geolocation = "Toronto";
            serviceContext.SetProfile(contentTransferProfile);

            if (!String.IsNullOrEmpty(secondaryRepository))
            {
                RepositoryIdentity repoId1 = new RepositoryIdentity();
                repoId1.RepositoryName = secondaryRepository;
                repoId1.UserName = userName;
                repoId1.Password = password;
                serviceContext.AddIdentity(repoId1);
            }
        }

        private string defaultRepository = "DMS";
        private string secondaryRepository = "DMS";
        private string userName = null;
        private string password = null;
        private IServiceContext serviceContext;
        private IObjectService objectService;
        private IQueryService queryService;



        private void m_getUrlButton_Click(object sender, EventArgs e)
        {
            userName = m_username.Text;
            password = m_password.Text;
            defaultRepository = m_repository.Text;

            initializeContext();
            ServiceFactory serviceFactory = ServiceFactory.Instance;
            objectService = serviceFactory.GetRemoteService<IObjectService>(serviceContext);
            queryService = serviceFactory.GetRemoteService<IQueryService>(serviceContext);

            List<String> streamingUrls = new List<string>();
            String mfile_url = null;

            try
            {
                ObjectIdentitySet objIdSet = new ObjectIdentitySet();
                ObjectIdentity objId = new ObjectIdentity();
                objId.RepositoryName = "DMS";
                objId.ValueType = ObjectIdentityType.QUALIFICATION;

                PassthroughQuery query = new PassthroughQuery();
                query.Repositories.Add("DMS");
                query.QueryString = "SELECT r_object_id, mfile_url('wmv', -1, '') FROM vd_episode_version WHERE v_archive_number = 'P036202-1-1-W'";

                QueryExecution queryExec = new QueryExecution(0, 10, 6);
                OperationOptions opOpts = new OperationOptions();

                QueryResult results = queryService.Execute(query, queryExec, opOpts);

                foreach (Emc.Documentum.FS.DataModel.Core.DataObject dataObj in results.DataObjects)
                {
                    PropertySet attrPropertySet = dataObj.Properties;
                    Property attrProperty = attrPropertySet.Properties[1];
                    StringArrayProperty attrStrProperty = (StringArrayProperty)attrProperty;

                    if (attrStrProperty.Values != null && attrStrProperty.Values.Count > 0)
                    {
                        mfile_url = attrStrProperty.Values[0];
                    }


                }

                String tempUrl = mfile_url.Replace("path=", "");
                tempUrl = tempUrl.Substring(0, tempUrl.IndexOf('&'));
                tempUrl = tempUrl.Replace('\\', '/');

                m_WMP.URL = tempUrl;


            }
            catch (ServiceException ex)
            {
                //Console.WriteLine(e.Message);
                MessageBox.Show(ex.ToString());
            }

        }
    }
}
